// pages/search/search.js
let QQMapWX = require('../../pages/qqmap-wx-jssdk1.1/qqmap-wx-jssdk');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: 'EUKBZ-JMMC2-EYCUG-CWPX5-UFMH7-TDBUR' // 必填
});
Page({
    data: {
        // 和风天气key
        myKey: 'a70e794a1ab0476782569a15b7150b37',
        // 搜索页面的数组
        searchCityName: [],
        // 定义一个flag，用于判断页面搜索是否有内容，有的话隐藏掉推荐城市和历史记录
        flag: true,
        hotcitys: [
            { city: '北京市', location: '116.40528,39.90498' },
            { city: '广州市', location: '113.23,23.16' },
            { city: '深圳市', location: '114.08594,22.54700' },
            { city: '上海市', location: '121.47264,31.23170' },
            { city: '南京市', location: '118.80870,32.10214' },
            { city: '杭州市', location: '120.15357,30.28745' },
            { city: '武汉市', location: '114.40071,30.50425' },
        ],
        historyArr: []
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    //定位当前城市
    onLocation: function () {
        wx.redirectTo({
            url: '/pages/index/index',
        })
    },
    // 点击搜索的城市信息跳转首页
    toIndex2: function (e) {
        console.log(e.currentTarget.dataset.city)
        let cityName = e.currentTarget.dataset.city
        let location = e.currentTarget.dataset.location.lng + ',' + e.currentTarget.dataset.location.lat
        wx.redirectTo({
            url: `/pages/index/index?location=${location}`,
            success: res => {
                console.log(res)
                this.data.historyArr.push({
                    city: cityName,
                    location: location
                })
                wx.setStorageSync('historyCity', this.data.historyArr)
                console.log(this.data.historyArr)
            }
        })
    },
    onShow: function () {
        let getHistoryArr = wx.getStorageSync('historyCity')
        console.log(getHistoryArr)
        if (getHistoryArr != '') {
            //数组对象去重
            let arr = getHistoryArr
            function deWeight() {
                for (let i = 0; i < arr.length - 1; i++) {
                    for (let j = i + 1; j < arr.length; j++) {
                        if (arr[i].city == arr[j].city) {
                            arr.splice(j, 1);
                            //因为数组长度减小1，所以直接 j++ 会漏掉一个元素，所以要 j--
                            j--;
                        }
                    }
                }
                return arr;
            }
            var newArr = deWeight();
            console.log(newArr);
            this.setData({
                historyArr: newArr
            })
        }
    },
    // 删除历史记录
    del: function () {
        wx.removeStorageSync('historyCity')
        this.setData({
            historyArr: []
        })
    },
    //取消点击事件
    cancel: function () {
        wx.redirectTo({
            url: '/pages/index/index',
        })
    },
    // 点击热门城市跳转首页
    toIndex: function (e) {
        console.log(e.currentTarget.dataset.location)
        console.log(e.currentTarget.dataset.city)
        wx.redirectTo({
            url: `/pages/index/index?location=${e.currentTarget.dataset.location}`,
            success: res => {
                this.data.historyArr.push({
                    city: e.currentTarget.dataset.city,
                    location: e.currentTarget.dataset.location
                })
                wx.setStorageSync('historyCity', this.data.historyArr)
                console.log(this.data.historyArr)
            }

        })
    },
    // 搜索城市
    searchCity: function (e) {
        let this_ = this
        // 调用接口
        qqmapsdk.getSuggestion({
            keyword: `${e.detail.value}`,
            complete: function (res) {
                // console.log(res.data);
                // 定义一个临时变量
                let searchName;
                let flag = true
                //如果有内容就正常赋值，没有内容就赋值一个空的，用来清空页面之前留下的内容
                if (res.data != undefined) {
                    searchName = res.data
                    flag = false
                } else {
                    searchName = ''
                    flag = true
                }
                this_.setData({
                    searchCityName: searchName,
                    flag: flag
                })
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})